package pe.com.bbva.mantenimiento.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.mantenimiento.dao.BinDAO;
import pe.com.bbva.mantenimiento.domain.Bin;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;

@Service("binDAO")
public class BinDAOImpl extends GenericDAOImpl<Bin> implements BinDAO{
	
	@Autowired
	public BinDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public List<Bin> findBins(Bin bin)throws BOException, DAOException {
		String where = "";
		if(bin.getDescripcion()!= null && !bin.getDescripcion().equals("")){
			where = where + (where==""?"":" AND ") + "upper(descripcion) like upper('%" + bin.getDescripcion() + "%') ";
		}
		if(bin.getCodigo()!= null && !bin.getCodigo().equals("")){
			where = where + (where==""?"":" AND ") + "upper(codigo) like upper('%" + bin.getCodigo() + "%') ";
		}
		if(bin.getFlagPriority()!= null && !bin.getFlagPriority().equals("")){
			where = where + (where==""?"":" AND ") + "flagPriority  = '" + bin.getFlagPriority() + "' ";
		}
		if(bin.getEstado()!= null && !bin.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado  = '" + bin.getEstado() + "' ";
		}
		if(bin.getId()!= null && !bin.getId().equals("")){
			where = where + (where==""?"":" AND ") + "id = '" + bin.getId() + "%' ";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by fechaCreacion desc,codigo, descripcion";
		List<Bin> listaBins = super.executeQuery(Bin.class,where,orders);
		return listaBins;
		}
}